<?php
class statistics extends core {
	private $collect_mod;
	
	public function __construct() {
		parent::__construct();
		$this->collect_mod = m('collect');
	}

	public function member() {
		$year = (isset($_GET['year']) && trim($_GET['year'])) ? trim($_GET['year']) : '';
		$month = (isset($_GET['month']) && trim($_GET['month'])) ? trim($_GET['month']) : '';
		if(!$year){
			$year = date('Y');
		}
		if(!$month){
			$month = date('m');
		}
		$premonth = $month - 1;
		$num = date("t",strtotime($year."-".$month));
		$arr = array();
		$days = array();
		for ($i=1; $i <= $num; $i++) {
			$days[] = "'".$i."'";
		}
		$days = implode(',', $days);
		for($i=0;$i<$num;$i++){
			$dateStr = $year."-".$month."-".$i;
			$starttime = strtotime($dateStr);
			$endtime = $starttime + 86399;
			$sql = "SELECT count(*) as num FROM {$this->tbp}member WHERE reg_time >= {$starttime} AND reg_time <= {$endtime}";
			$a = $this->db->get_var($sql);
			$arr[] =$a;
			// $arr[] =strval($a);
		}
		$rs = implode(',', $arr);
		$this->smarty->assign("rs",$rs);
		$this->smarty->assign("year",$year);
		$this->smarty->assign("month",$month);
		$this->smarty->assign("days",$days);
		$this->display();
	}
	
	public function order() {
		$city_id = $this->request->get('city_id', 0);
		$member_type = $this->request->get('member_type');
		$begin_time = $this->request->get('begin_time');
		$end_time = $this->request->get('end_time');
		$begintime = strlen($begin_time) ? $begin_time : date('Y-m');
		$endtime = strlen($end_time) ? $end_time : date('Y-m');
		$xaxis = array();
		$total_price = array();
		$arr = array();
		$months = $this->collect_mod->getMonthRange($begintime, $endtime);
		$starttime = 0;
		$overtime = 0;
		$num = count($months);
		$days = get_date_diff($begintime, $endtime);
		$days = $days['days'];
		if ($days<=31) {
			for ($i=0; $i<=$days; $i++) {
				$date = date('d', strtotime("+{$i} day", strtotime($begintime)));
				$xaxis[] = "'{$date}日'";
			}
		} else if (count($months)>0) {
			for ($i=0; $i<$num; $i++) {
				$xaxis[] = "'".$months[$i]."'";
			}
		}
		$_xaxis = implode(',', $xaxis);
		for ($i=0; $i<count($xaxis); $i++) {
			if ($days<=31) {
				$starttime = strtotime("+{$i} day", strtotime($begintime));
				$overtime = $starttime + (60*60*24-1);
			} else {
				$starttime = strtotime($months[$i].'-1');
				$overtime = strtotime($months[$i].'-'.date('t', strtotime($months[$i])).' 23:59:59');
			}
			$where = "status>0 AND begin_time>={$starttime} AND begin_time<={$overtime}";
			if ($city_id>0) $where .= " AND city_id='{$city_id}'";
			if (strlen($member_type)) $where .= " AND member_type='{$member_type}'";
			$price = floatval(SQL::share('collect')->where($where)->val('SUM(total_price)'));
			$total_price[] = $price;
			$arr[] = SQL::share('collect')->where($where)->count();
		}
		$total_price = implode(',', $total_price);
		$rs = implode(',', $arr);
		$this->smarty->assign("xaxis",$_xaxis);
		$this->smarty->assign("total_price",$total_price);
		$this->smarty->assign("rs",$rs);
		$this->smarty->assign('city', $this->collect_mod->get_city());
		$this->smarty->assign('city_id', $city_id);
		$this->smarty->assign('member_type', $member_type);
		$this->smarty->assign('begin_time', $begin_time);
		$this->smarty->assign('end_time', $end_time);
		$this->display();
	}
	public function sales() {
		$tol='';
		$year = (isset($_GET['year']) && trim($_GET['year'])) ? trim($_GET['year']) : '';
		$month = (isset($_GET['month']) && trim($_GET['month'])) ? trim($_GET['month']) : '';
		$sort = (isset($_GET['sort']) && trim($_GET['sort'])) ? intval($_GET['sort']) : 0;
		if($sort == 2){
			$sort = 'quantity';
		}else{
			$sort = 'price';
		}
		if(!$year){
			$year = date('Y');
		}
		if(!$month){
			$month = date('m');
		}
		$dateStr = $year."-".$month;
		$starttime = strtotime($dateStr);
		$months = $month + 1;
		$endStr = $year."-".$months;
		$endtime = strtotime($endStr);
		$endtime = $endtime - 1;
		//分页
		$this->setpages(array('year'=>$year, 'month'=>$month, 'sort'=>$sort));
		$sql = "SELECT goods_name,goods_id,sum(price*quantity) as price,sum(quantity) as quantity FROM {$this->tbp}order_goods 
			WHERE order_id IN (SELECT id FROM {$this->tbp}order o WHERE o.status > 0 AND o.add_time >= {$starttime}
			AND o.add_time <={$endtime}) GROUP BY goods_id ORDER BY {$sort} DESC";
		$tol = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign("row",$tol);
		$this->smarty->assign("page",$nav);
		$this->smarty->assign("year",$year);
		$this->smarty->assign("month",$month);

		$this->display();
	}
}
